<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

use Models\Admin;
class AuthController extends CI_Controller {

	
	public function login(){
		$this->load->view('admin/auth/login');
	}

	public function logout(){
		session_destroy();
		$this->login();
	}

	public function check(){

		$account = $this->input->post('account');
		if(!$account){
			echo json_encode(array('code'=>1,'msg'=>'请输入您的账号！'));
			exit;
		}

		$password = $this->input->post('password');
		if(!$password){
			echo json_encode(array('code'=>1,'msg'=>'请输入您的密码'));
			exit;
		}

		$admin = new Admin;
		$admin = $admin->login($account,$password);
		if($admin === FALSE){
			echo json_encode(array('code'=>1,'msg'=>'账号或密码不正确'));
			exit;
		}

		$array = array(
			'admin_login' => 1,
			'admin_id'=>$admin['id'],
			'admin_account'=>$admin['account'],
			'admin_permissions'=>$admin['permissions'],
			'admin_login_time'=>date('Y-m-d H:i:s'),
			'admin_language'=>'chinese'
		);
		
		$this->session->set_userdata( $array );

		echo json_encode(array('code'=>0,'msg'=>'ok'));
	}

	public function lang(){
		if($this->session->admin_language == 'english'){
			$this->session->set_userdata('admin_language','chinese');
		}else{
			$this->session->set_userdata('admin_language','english');
		}
	}

}

?>